package com.emm.yixun.system.service.impl.notice.pricescheme;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.DictionaryService;
import com.emm.yixun.basic.service.PriceSchemeService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.basic.service.ProjectService;
import com.emm.yixun.basic.service.ProjectSettingService;
import com.emm.yixun.basic.service.SubscribeService;
import com.emm.yixun.basic.service.TransactionRelationService;
import com.emm.yixun.basic.service.UserService;
import com.emm.yixun.common.enums.ResultEnum;
import com.emm.yixun.common.model.Dictionary;
import com.emm.yixun.common.model.PriceScheme;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.ProjectSetting;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.DictionaryDto;
import com.emm.yixun.common.model.dto.NoticeEntity;
import com.emm.yixun.common.model.dto.ProjectSettingDto;
import com.emm.yixun.common.model.dto.UserDto;
import com.emm.yixun.common.model.enums.ReportMessageEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.utils.MsgUtil;
import com.emm.yixun.system.service.impl.manage.SysNoticeManage;
import com.emm.yixun.system.service.notice.pricescheme.PriceSchemeValidNoticeFacade;

/**
 * 价格方案审批消息
 * @author jhsong
 *
 */
@Service("priceSchemeValidNoticeFacade")
@WebService(name="com.emm.yixun.system.service.PriceSchemeValidNoticeFacade")
public class PriceSchemeValidNoticeFacadeImpl implements PriceSchemeValidNoticeFacade{
	private  final Logger   logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SubscribeService subscribeService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private UserService userService;
	@Autowired
	private TransactionRelationService transactionRelationService;
	@Autowired
	private ProjectSettingService projectSettingService;
	@Autowired
	private ProjectRoomService projectRoomService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private SysNoticeManage sysNoticeManage;
	@Autowired
	private PriceSchemeService priceSchemeService;
	
	@Override
	public Response<Boolean> createPriceSchemeValidNotice(NoticeEntity noticeEntity) {
		Response<Boolean> response = new Response<Boolean>();
		try {
			// 实体对象
			PriceScheme priceScheme = priceSchemeService.selectById(noticeEntity.getNoticeBusinessId());
			// 项目
			Project project = projectService.selectById(priceScheme.getProjectId());
			// 发起人
			UserDto entity = new UserDto();
			entity.setUserCode(priceScheme.getUserCode());
			User user = userService.selectByDto(entity);
			String userName_fqr = user==null?"":user.getUserName();
			List<User> userList = new ArrayList<User>();
			userList.add(user);
			if(userList.size() > 0){
				assemblePriceSchemeValidNotice(priceScheme, project, userList, "1", null, noticeEntity.getProcessInstanceId());
			}
			
			// 营销负责人【销管经理】
			List<User> userList_jgfa = new ArrayList<User>();
			ProjectSettingDto pDto = new ProjectSettingDto();
			pDto.setProjectCode(project.getProjectCode());
			List<ProjectSetting> pObj = projectSettingService.selectListByDto(pDto);
			User saleLeaderUser = userService.selectById(pObj.get(0).getMaLeader());
			userList_jgfa.add(saleLeaderUser);
			//审核通过，组装报备消息内容
			if(userList_jgfa.size() > 0 && "10".equals(priceScheme.getStatus())){
				assemblePriceSchemeValidNotice(priceScheme, project, userList_jgfa, "2", userName_fqr, noticeEntity.getProcessInstanceId());
			}
			response.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			response.setSuccess(false);
			logger.error("组装价格方案消息异常:{}",e);
		}
		return response;
	}
	
	/**
	 * 组装价格方案消息
	 * 
	 * @param payDeposit 业务对象
	 * @param userList 用户列表
	 * @param type 1 审核结果 2 报备信息
	 * @param userName_fqr 发起人
	 * @param processInstanceId 流程ID
	 * @return
	 */
	public String  assemblePriceSchemeValidNotice(PriceScheme priceScheme, Project project, List<User> userList, String type, String userName_fqr, String processInstanceId){
		Map<String, String> noticeParamMap = new HashMap<String, String>();
		try {
			// 价格方案流程, 获取字典表dictionary_value = 'NT36' 的消息模板
			// 消息模板: 价格方案审核结果【${auditStatus}】方案名称：${schemeName}
			String msgTemp = "";
			if("1".equals(type)){
				msgTemp = "价格方案审核结果【${auditStatus}】方案名称：${schemeName}";
			}else{
				//msgTemp = "【价格方案】：${schemeName}；发起人：${userName}【审核通过】";
				msgTemp = ReportMessageEnum.JGFA.getDes();
			}
			String sendMsg = "";
			DictionaryDto dicDto = new DictionaryDto();
			dicDto.setDictionaryValue("NT36");
			Dictionary dic = dictionaryService.selectByDto(dicDto);
			String status = "";
			if ("10".equals(priceScheme.getStatus()) || "01".equals(priceScheme.getStatus())) {
				status = "已通过";
			}else{
				status = "已退回";
			}
			if("1".equals(type)){
//				if (dic != null) {
//					msgTemp = dic.getDictionaryName();
//				}
				sendMsg = msgTemp.replace("${auditStatus}", status).replace("${schemeName}", priceScheme.getName());
			}else{
				sendMsg = msgTemp.replace("${schemeName}", priceScheme.getName()).replace("${userName}", userName_fqr);
			}
			// 创建提醒消息及消息发送人信息
			noticeParamMap.put("noticeDicId",  dic.getId()+"");
			noticeParamMap.put("noticeTempType", "NT36");
			noticeParamMap.put("sendMsg", sendMsg);
			noticeParamMap.put("title", "价格方案流程审核结果");
			noticeParamMap.put("merchantId", String.valueOf(priceScheme.getMerchantId()));
			noticeParamMap.put("userCode", priceScheme.getUserCode());
			noticeParamMap.put("accessCode", priceScheme.getAccessCode());
			noticeParamMap.put("createUserId", String.valueOf(priceScheme.getCreateUserId()));
			noticeParamMap.put("businessId", String.valueOf(priceScheme.getId()));
			noticeParamMap.put("projectCode", project.getProjectCode());
			if("1".equals(type)){
				noticeParamMap.put("tempType", "2");
			}else{
				noticeParamMap.put("tempType", "3");
			}
			noticeParamMap.put("msgOrder", "1");
			/*
			 * (ym) 提醒内容增加超链接 start
			 * */
			Map<String,String> replaceValuesMap=new HashMap<String,String>();
			replaceValuesMap.put(MsgUtil.ROOM_ID, "");
			replaceValuesMap.put(MsgUtil.ROOM_NAME, "");
			replaceValuesMap.put(MsgUtil.CUSTOMER_ID, "");
			replaceValuesMap.put(MsgUtil.CUSTOMER_NAME,"");
			replaceValuesMap.put(MsgUtil.PRICE_SCHEME_ID, String.valueOf(priceScheme.getId()));
			replaceValuesMap.put(MsgUtil.PRICE_SCHEME_NAME, priceScheme.getName());
			replaceValuesMap.put(MsgUtil.BUSINESS_ID, priceScheme.getId()+"");
			replaceValuesMap.put(MsgUtil.NEXT_NODE_ID, "");
			replaceValuesMap.put(MsgUtil.PROCESS_INSTANCE_ID, processInstanceId+"");
	    	noticeParamMap.put("replaceValues", MsgUtil.getReplaceValues(replaceValuesMap));
	    	/*
			 * (ym) 提醒内容增加超链接 end
			 * */
	    	sysNoticeManage.createNoticeWithNoticeTemp(noticeParamMap, userList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("组装价格方案消息异常:{}",e);
		}
		return ResultEnum.SUCCESS.getNo();
	}
	
}
